<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form class="reg" :model="formData" :rules="formRules" ref="formRef">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-user-solid"
            v-model="formData.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="formData.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            placeholder="请确认密码"
            prefix-icon="el-icon-lock"
            v-model="formData.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submitForm"
            >注册</el-button
          >
          <el-link>去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Reg",
  data() {
    // callback();
    // 如果校验失败,callback(new Error('错误的描述'))
    // 如果校验成功,callback() 放行代码,如果没有使用,那么程序会卡住不执行,校验无法获取结果

    // 自定义校验规则函数,校验两次密码是否相同
    var samePwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.formData.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据双向绑定
      formData: {
        username: "",
        password: "",
        repassword: "",
      },

      // 表单数据校验
      formRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^[0-9a-zA-Z]{1,10}$/,
            message: "请输入长度1-10的字母数字",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "请输入6-15位非空字符串",
            trigger: "blur",
          },
        ],
        repassword: [
          {
            pattern: /^\S{6,15}$/,
            message: "请输入6-15位非空字符串",
            trigger: "blur",
          },
          {
            // 调用校验规则函数
            validator: samePwd,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      // 在form标签中用ref声明
      // 再通过this.$refs.formRef获取form的dom元素
      // 使用form调用validate(函数)
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post(
            "/api/reg",
            this.formData
          );
          console.log(res);
          if (res.code === 0) {
            // 成功弹窗提示
            this.$message.success(res.message);
            // 跳转到登录页面
            this.$router.push("/login");
          } else {
            // 注册失败提示
            this.$message.error(res.message);
            // 注册失败;
            console.log("失败");
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.reg-container {
  background: url("../../assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }

    .reg {
      padding-left: 20px;
      padding-right: 20px;

      .btn {
        width: 100%;
      }
    }
  }
}
</style>